﻿using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections.Generic;
using System.Text;

namespace APP.QUANLY.Common
{
    public class RegistrySetting
    {
        private const string RMASS = "RMASS";
        private const string RMASD = "RMASD";
        private const string RMASU = "RMASU";
        private const string RMASP = "RMASP";
        private const string RMASSe = "RMASSe";
        private const string RMASLi = "RMASLi";
        private const string RMASY = "RMASY";
        private const string RMASDB = "RMASDB";
        private const string RMASMB = "RMASMB";
        private const string RMASYB = "RMASYB";
        private const string RMASC = "RMASC";
        private static string ValueConvert = "RManager";
        private const string RMAS_REGKEYA = @"SOFTWARE\HLV\RMAS";
        private const string RMAS_REGKEYB = @"SOFTWARE\Microsoft\RMAS";
        public string Guid = "A948FD4F-4C54-49DF-8FF5-015DCC6CE32C";
        protected AppSetting app;
        private const int nDayLimitDemo = 30;

        public RegistrySetting()
        {
            app = AppSetting.getSetting();
            app.c_RegistryKeyA = RMAS_REGKEYA;
            app.c_RegistryKeyB = RMAS_REGKEYB;
        }
        private void GetRegValue(out string Server, out string Database, out string UserName, out string Pass, out string SerialKey, out string LicenceKey, out string Yes, out string DayBegin, out string MonthBegin, out string YearBegin, out string Count)
        {
            Server = TCEncryption.ToDecrypt(app.getRegValue(RMASS));
            Database = TCEncryption.ToDecrypt(app.getRegValue(RMASD));
            UserName = TCEncryption.ToDecrypt(app.getRegValue(RMASU));
            Pass = TCEncryption.ToDecrypt(app.getRegValue(RMASP));
            SerialKey = app.getRegValue(RMASSe);
            LicenceKey = app.getRegValue(RMASLi);
            Yes = TCEncryption.ToDecrypt(app.getRegValue(RMASY));
            DayBegin = TCEncryption.ToDecrypt(app.getRegValue(RMASDB));
            MonthBegin = TCEncryption.ToDecrypt(app.getRegValue(RMASMB));
            YearBegin = TCEncryption.ToDecrypt(app.getRegValue(RMASYB));
            Count = TCEncryption.ToDecrypt(app.getRegValue(RMASC));
        }
        private void SetRegValue(string Server, string Database, string UserName, string Pass, string SerialKey, string LicenceKey, string Yes, string DayBegin, string MonthBegin, string YearBegin, string Count)
        {
            app.setRegValue(RMASS, TCEncryption.ToEncrypt(Server));
            app.setRegValue(RMASD, TCEncryption.ToEncrypt(Database));
            app.setRegValue(RMASU, TCEncryption.ToEncrypt(UserName));
            app.setRegValue(RMASP, TCEncryption.ToEncrypt(Pass));
            app.setRegValue(RMASSe, SerialKey);
            app.setRegValue(RMASLi, LicenceKey);
            app.setRegValue(RMASY, TCEncryption.ToEncrypt(Yes));
            app.setRegValue(RMASDB, TCEncryption.ToEncrypt(DayBegin));
            app.setRegValue(RMASMB, TCEncryption.ToEncrypt(MonthBegin));
            app.setRegValue(RMASYB, TCEncryption.ToEncrypt(YearBegin));
            app.setRegValue(RMASC, TCEncryption.ToEncrypt(Count));
        }
        private void SetRegValueLicence(string SerialKey, string LicenceKey, string Yes, string DayBegin, string MonthBegin, string YearBegin, string Count)
        {
            app.setRegValue(RMASSe, SerialKey);
            app.setRegValue(RMASLi, LicenceKey);
            app.setRegValue(RMASY, TCEncryption.ToEncrypt(Yes));
            app.setRegValue(RMASDB, TCEncryption.ToEncrypt(DayBegin));
            app.setRegValue(RMASMB, TCEncryption.ToEncrypt(MonthBegin));
            app.setRegValue(RMASYB, TCEncryption.ToEncrypt(YearBegin));
            app.setRegValue(RMASC, TCEncryption.ToEncrypt(Count));
        }

        private SoftLicence GetSoftLicence()
        {
            string Server = "", Database = "", UserName = "", Pass = "",  SerialKey = "",  LicenceKey = "",  Yes = "",  DayBegin = "",  MonthBegin = "",  YearBegin = "",  Count = "";
            SoftLicence SL = new SoftLicence();
            try
            {
                GetRegValue(out Server, out Database, out UserName, out Pass, out SerialKey, out LicenceKey, out Yes, out DayBegin, out MonthBegin, out YearBegin, out Count);
            }
            catch
            {
                SL = null;
                return SL;
            }

            if (Server == "" || Database == "" || UserName == "" || Pass == "" || SerialKey == "" || LicenceKey == "" || Yes == "" || DayBegin == "" || MonthBegin == "" || YearBegin == "" || Count == "")
            {
                SL = null;
                return SL;
            }
            else
            {
                SL.SerialKey = SerialKey;
                SL.LicenceKey = LicenceKey;
                if (Yes == "YES")
                    SL.IsDemo = true;
                else
                    SL.IsDemo = false;
                try
                {
                    DateTime time = Conversions.ToDate(YearBegin + "-" + MonthBegin + "-" + DayBegin);
                    SL.DtBegin = time;
                }
                catch
                {
                    DateTime time = new DateTime(1990, 11, 11);
                    SL.DtBegin = time;
                }
                SL.Count = Int32.Parse(Count);
            }
            return SL;
        }
        private SoftLicence SetSoftLicence()
        {
            string Server = app.getConfigValue("ip_server");
            string Database = app.getConfigValue("database_server");
            string UserName = app.getConfigValue("user_server");
            string Pass = TCEncryption.ToDecrypt(app.getConfigValue("password_server"));
            string SerialKey = "";
            SoftLicence SL = GetSoftLicence();
            if (SL == null)//RunFirst
            {
                TrialLicence SoftLicence = new TrialLicence("RMAS", ValueConvert, Guid);
                SerialKey = SoftLicence.SetSerialKey(RunTypes.Trial, false);

                SL = new SoftLicence();
                SL.SerialKey = SerialKey;
                SL.LicenceKey = SerialKey;
                SL.IsDemo = true;
                DateTime time = DateTime.Now;
                SL.DtBegin = time;
                SL.Count = 30;
                SetRegValue(Server, Database, UserName, Pass, SerialKey, SerialKey, "YES", time.Day.ToString(), time.Month.ToString(), time.Year.ToString(), nDayLimitDemo.ToString());
            }
            return SL;
        }
        
        public SoftLicence RunFirst()
        {
            return GetSoftLicence() == null ? null : SetSoftLicence();
        } //Thay thế hàm SetRegValueLicence30RunFirst
        public SoftLicence RunContinue()
        {
            return GetSoftLicence() == null ? SetSoftLicence() : null;
        }
        private RunTypes CheckRunType(string SerialKey, out SoftLicence SL)
        {
            RunTypes runtypes = RunTypes.UnKnown;
            SL = GetSoftLicence();
            if (SL == null)
                return RunTypes.UnKnown;
            if (SL.LicenceKey == SerialKey && SL.IsDemo == true && (SL.Count > 0 && SL.Count <= 30))
            {
                int num = 0;
                TimeSpan ts = DateTime.Now - SL.DtBegin;
                num = ts.Days;
                //num = (int)DateAndTime.DateDiff(DateInterval.Month, SL.DtBegin, DateAndTime.Now, FirstDayOfWeek.System, FirstWeekOfYear.System);
                if (num > 30)
                {
                    runtypes = RunTypes.Expired;
                }
                if(num >= 0 && num <= 30)
                {
                    SL.Count -= num;
                    runtypes = RunTypes.Trial;
                }
            }
            if (SL.LicenceKey != SerialKey && SL.IsDemo == false && SL.Count == -1)
            {
                runtypes = RunTypes.Full;
            }
                
            return runtypes;
        }
        public RunTypes CheckDemo(string SerialKey, out SoftLicence SL)
        {
            RunTypes runtypes = RunTypes.UnKnown;
            runtypes = CheckRunType(SerialKey, out SL);
            switch (runtypes)
            {
                case RunTypes.UnKnown:
                    runtypes = RunTypes.UnKnown;
                    SL = null;
                    break;
                case RunTypes.Trial:
                    SetRegValueLicence(SL.SerialKey, SL.LicenceKey, (SL.IsDemo == true) ? "YES" : "NO", SL.DtBegin.Day.ToString(), SL.DtBegin.Month.ToString(), SL.DtBegin.Year.ToString(), SL.Count.ToString());
                    break;
                case RunTypes.Expired:
                    runtypes = RunTypes.Expired;
                    //SL = null;
                    break;
                case RunTypes.Full:
                    string LicenceKey = "";
                    TrialLicence Slicence = new TrialLicence("RMAS", ValueConvert, Guid);
                    LicenceKey = Slicence.SetLicenceKey(SerialKey);
                    if (LicenceKey == SL.LicenceKey)
                        runtypes = RunTypes.Full;
                    else 
                        runtypes = RunTypes.UnKnown;
                    break;
            }
            return runtypes;
        }
        public bool CheckActive(SoftLicence SL)
        {
            string LicenceKey = "";
            SoftLicence SLBase = new SoftLicence();
            RunTypes runTypes = CheckDemo(SL.SerialKey, out SLBase);

            if (runTypes == RunTypes.Expired || runTypes == RunTypes.Trial)
            {
                TrialLicence Licence = new TrialLicence("RMAS", ValueConvert, Guid);
                LicenceKey = Licence.SetLicenceKey(SL.SerialKey);
                if (SL.LicenceKey == LicenceKey)
                {
                    SetRegValueLicence(SL.SerialKey, SL.LicenceKey, "NO", "11", "11", "1990", "-1");
                    runTypes = CheckRunType(SL.SerialKey, out SLBase);
                    if (runTypes == RunTypes.Full)
                        return true;
                    else
                    {
                        SetRegValueLicence(SL.SerialKey, SL.SerialKey, "YES", SL.DtBegin.Day.ToString(), SL.DtBegin.Month.ToString(), SL.DtBegin.Year.ToString(), SL.Count.ToString());
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        //public RunTypes CheckDemo(string SerialKeyUI, out string TrialDay)
        //{
        //    RunTypes runtypes = RunTypes.UnKnown;
        //    int num = 0;
        //    string Server = "", Database = "", UserName = "", Pass  = "", SerialKey = "", LicenceKey= "", Yes = "", DayBegin = "", MonthBegin = "", YearBegin = "", Count = "";
        //    string CompareLicenceKey = "";
            
        //    TrialDay = "";

        //    try
        //    {
        //        GetRegValue(out Server, out Database, out UserName, out Pass, out SerialKey, out LicenceKey, out Yes, out DayBegin, out MonthBegin, out YearBegin, out Count);
        //        //Kiểm tra giá trị không null
        //        if (Server == "" || Database == "" || UserName == "" || Pass == "" || SerialKey == "" || LicenceKey == "" || Yes == "" || DayBegin == "" || MonthBegin == "" || YearBegin == "" || Count == "")
        //        {
        //            return RunTypes.UnKnown;
        //        }
        //        else
        //        {
                    
        //        }
        //        if (Yes == "Yes".ToUpper()) // Đang ở dạng demo
        //        {
        //            if (SerialKeyUI == SerialKey) // So SerialKey từ trong registry với giao diện
        //            {
        //            }
        //            else // Không giống registry giữa giao diện và registry.
        //            {
        //                return RunTypes.UnKnown;
        //            }
        //        }
        //        if (SerialKeyUI == SerialKey)
        //        {
        //            if (SerialKey == LicenceKey)
        //            {
        //                runtypes = RunTypes.Trial;
        //                TrialDay = "";
        //            }
        //            else
        //            {
        //                TrialLicence SoftLicence = new TrialLicence("RMAS", ValueConvert, Guid);
        //                CompareLicenceKey = SoftLicence.SetLicenceKey(SerialKey);
        //                if (LicenceKey == CompareLicenceKey)
        //                {
        //                    runtypes = RunTypes.Full;
        //                    //LK = LicenceKey;
        //                }
        //                runtypes = RunTypes.UnKnown;
        //                TrialDay = "";
        //            }
        //        }
        //        else
        //        {
        //            runtypes = RunTypes.UnKnown;
        //            TrialDay = "";
        //        }
        //    }
        //    catch(Exception ex)
        //    {
        //        runtypes = RunTypes.UnKnown;
        //        TrialDay = "";
        //        return runtypes;
        //    }

        //    if (runtypes == RunTypes.Trial)
        //    {
        //        if (DayBegin != "" || MonthBegin != "" || YearBegin != "")
        //        {
        //            try
        //            {
        //                DateTime time = Conversions.ToDate(YearBegin + "-" + MonthBegin + "-" + DayBegin);
        //                num = (int)DateAndTime.DateDiff(DateInterval.Month, time, DateAndTime.Now, FirstDayOfWeek.System, FirstWeekOfYear.System);
        //                if (num > 0)
        //                {
        //                    Interaction.MsgBox("Thời gian dùng thử đã hết, xin vui lòng liên hệ tới số (0128.289.2832)", MsgBoxStyle.Critical, null);
        //                    TrialDay = "";
        //                    runtypes = RunTypes.Expired;
        //                }
        //                else
        //                {
        //                    TrialDay = (Int32.Parse(Count) - num).ToString();
        //                    SetRegValue(Server, Database, UserName, Pass, SerialKey, LicenceKey, Yes, DayBegin, MonthBegin, YearBegin, TrialDay);
        //                    runtypes = RunTypes.Trial;
        //                }
        //            }
        //            catch
        //            {
        //                runtypes = RunTypes.UnKnown;
        //                TrialDay = "";
        //            }
        //        }
        //        else
        //        {
        //            TrialDay = "";
        //            runtypes = RunTypes.UnKnown;
        //        }
        //        return runtypes;
        //    }
        //    else
        //    {
        //        if (runtypes == RunTypes.Full)
        //        {
        //            runtypes = RunTypes.Full;
        //        }
        //        if (runtypes == RunTypes.Expired)
        //        {
        //            runtypes = RunTypes.Expired;
        //        }
        //        if (runtypes == RunTypes.UnKnown)
        //        {
        //            runtypes = RunTypes.UnKnown;
        //        }
        //    }
        //    TrialDay = "";
        //    return runtypes;
        //}

        //public void SetRegValueLicence30RunFirst(out string SerialKey, out string DayBegin, out string MonthBegin, out string YearBegin)
        //{
        //    string Server = app.getConfigValue("ip_server");
        //    string Database = app.getConfigValue("database_server");
        //    string UserName = app.getConfigValue("user_server");
        //    string Pass = TCEncryption.ToDecrypt(app.getConfigValue("password_server"));

        //    TrialLicence SoftLicence = new TrialLicence("RMAS", ValueConvert, Guid);
        //    SerialKey = SoftLicence.SetSerialKey(RunTypes.Trial, false);

        //    string LicenceKey = SerialKey;
        //    string Yes = "YES";

        //    DateTime dt = DateTime.Now;
        //    DayBegin = dt.Day.ToString();
        //    MonthBegin = dt.Month.ToString();
        //    YearBegin = dt.Year.ToString();

        //    SetRegValue(Server, Database, UserName, Pass, SerialKey, LicenceKey, Yes, DayBegin, MonthBegin, YearBegin, nDayLimitDemo.ToString());
        //}
        //public bool RunFirstTime()
        //{
        //    try
        //    {
        //        string Server = app.getRegValue(TCEncryption.ToDecrypt(RMASS));
        //        string Database = app.getRegValue(TCEncryption.ToDecrypt(RMASD));
        //        string UserName = app.getRegValue(TCEncryption.ToDecrypt(RMASU));
        //        string Pass = app.getRegValue(TCEncryption.ToDecrypt(RMASP));
        //        string SerialKey = app.getRegValue(TCEncryption.ToDecrypt(RMASSe));
        //        string Licence = app.getRegValue(TCEncryption.ToDecrypt(RMASLi));
        //        string Yes = app.getRegValue(TCEncryption.ToDecrypt(RMASY));
        //        string DayBegin = app.getRegValue(TCEncryption.ToDecrypt(RMASDB));
        //        string MonthBegin = app.getRegValue(TCEncryption.ToDecrypt(RMASMB));
        //        string YearBegin = app.getRegValue(TCEncryption.ToDecrypt(RMASYB));
        //        string Count = app.getRegValue(TCEncryption.ToDecrypt(RMASC));
        //        if (Server == "" || Database == "" || UserName == "" || Pass == "" || SerialKey == "" || Licence == "" || Yes == "" || DayBegin == "" || MonthBegin == "" || YearBegin == "")
        //            return true;
        //        else
        //            return false;
        //    }
        //    catch(Exception ex)
        //    {
        //        MessageBoxEx.Show("Không thể tải Registry Server: " + ex.Message, 10);
        //        return false;
        //    }
        //}

        
    }
}
